<template>
  <div class="station-search assessment-container" style="width:100%">
    <div class="table-container" style="margin:0">
      <el-table
        :data="dataLists"
        :span-method="arraySpanMethod"
        style="width: 100%"
        class="xpaas-table-class"
        row-class-name="xpaas-row-class"
        cell-class-name="xpaas-cell-class"
        header-row-class-name="xpaas-header-row-class"
        header-cell-class-name="xpaas-header-cell-class"
      >
        <el-table-column v-if="isOperation" prop="cz" label="操作" width="90" align="center">
          <!--想要合并，请在配置config里添加 cz-->
          <template slot-scope="scope">
            <!--<div class="add-button" @click="addHours(scope.row.courseId,url)" v-if="scope.row.checkStatus != '未填报' && scope.row.checkStatus != '不通过'">-->
              <!--<img src="../../assets/assessment/class-add-icon.png" alt="" width="15px" height="15px"/>-->
              <!--<span>录入</span>-->
            <!--</div>-->
            <div v-if="scope.row.checkStatus != '未填报'">
              <el-button @click="addHours(scope.row.courseId,url)" icon="el-icon-circle-plus-outline" type="warning" size="mini" plain>录入</el-button>
            </div>
            <div v-else></div>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, index) in colDataList"
          :label="item.label"
          :prop="item.prop"
          :key="index"
          :width="item.width"
          align="center"
          :render-header="labelHead"
        >
        </el-table-column>

      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  name: "tablelist",
  props: ["dataList", "colDataList", "config",'isOperation','url'],
  data() {
    return {
      isOperation:false,
      dataLists:''
    };
  },
  watch:{
    dataList: {
      deep: true,
      handler(nv, ov) {
        this.dataLists=nv
        this.mergeRow(nv);
        console.log(nv,ov)
      }
    }
  },
  methods: {
    addHours(id,url) {
      this.$router.push({
        path: url,
        query: {
          courseId: id
        }
      })
      // this.$router.push("/teacher/classHours/addHours");
    },
    labelHead: function (h, { column, $index }) {
      let l = column.label.length;
      let f = 16;
      column.minWidth = f * (l + 2); //加上一个文字长度
      return h("div", { class: "table-head", style: { width: "100%" } }, [
        column.label,
      ]);
    },
    isEmpty(val) {
      return typeof val == "undefined" || val === null || val == "-";
    },
    isObject(val) {
      return Object.prototype.toString.call(val) === "[object Object]";
    },
    arraySpanMethod({ row, column }) {
      const span = column["property"] + "-span";
      if (row[span]) {
        return row[span];
      }
    },
    mergeRow(dataLists) {

      let arr = [].concat(dataLists);
      // arr.splice(index, 0, Object.assign({}, row))

      this.dataLists = this.mergeData(arr);
    },
    delRow(index) {
      this.dataLists.splice(index, 1);
      this.dataLists = this.mergeData(this.dataLists);
    },
    mergeData(list) {
      // mergeTarge - 合并项依据
      // combineProp - 根据mergeTarge 进行合并的单元格属性
      return this.filterData(this.config, [].concat(list));
    },
    filterData(config, dataLists, targetProp) {
      const { mergeTarget, combineProp } = config;
      // 叠加的合并条件
      combineProp.forEach((column) => {
        // 记录合并项
        let combineCount = 1;
        if (this.isObject(column)) {
          return this.filterData.call(this, column, dataLists, mergeTarget);
        }
        // 使用倒序遍历
        for (let i = dataLists.length - 1; i >= 0; i--) {
          let curr = dataLists[i],
            prev = dataLists[i - 1];

          if (this.isEmpty(prev)) {
            // 已经循环完毕
            curr[`${column}-span`] = {
              rowspan: combineCount,
              colspan: combineCount >= 1 ? 1 : 0,
            };
            break;
          }

          if (
            prev[targetProp] === curr[targetProp] &&
            prev[mergeTarget] === curr[mergeTarget] &&
            prev[`${column}`] === curr[`${column}`]
          ) {
            ++combineCount;
            curr[`${column}-span`] = {
              rowspan: 0,
              colspan: 0,
            };
          } else {
            curr[`${column}-span`] = {
              rowspan: combineCount,
              colspan: combineCount >= 1 ? 1 : 0,
            };
            prev[`${column}-span`] = {
              rowspan: 1,
              colspan: 1,
            };
            // 前后项不相符时，对combinecount进行重置
            combineCount = 1;
          }
        }
      });
      return dataLists;
    },
  },
};
</script>
<style scoped lang="less">
@fontFamily:"Microsoft YaHei";
.xpaas-table-class {
  border: 1px solid #004ca7;
  border-radius: 9px;
  margin: 15px 0;
  > ::v-deep div.el-table__header-wrapper {
    > table.el-table__header {
      > thead {
        > tr.xpaas-header-row-class {
          padding: 0;
          height: 76px;
          background-color: #edf2f9;
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          line-height: 20px;
          color: #004ca7;
          > th.xpaas-header-cell-class {
            font-weight: bold;
            border-right: 1px solid #c7d7eb;
            border-left: 0;
            border-bottom: 1px solid #c7d7eb;
            background-color: #edf2f9;
            > div.cell {
              font-size: 16px;
              font-weight: bold;
              font-family: @fontFamily;
              color: #004ca7;
            }
          }
        }
      }
    }
  }
  > ::v-deep div.el-table__body-wrapper {
    > table.el-table__body {
      > tbody {
        > tr.xpaas-row-class {
          > td.xpaas-header-cell-class {
            border-right: 1px solid #c7d7eb;
            border-left: 0;
            border-bottom: 1px solid #c7d7eb;
            background-color: #edf2f9;
            > div.cell {
              font-size: 16px;
              font-weight: bold;
              font-family: @fontFamily;
              color: #004ca7;
            }
          }
          > td.xpaas-cell-class {
            height: 76px;
            border-right: 1px solid #c7d7eb;
            border-left: 0;
            border-bottom: 1px solid #c7d7eb;
            > div.cell {
              font-weight: normal;
              font-family: @fontFamily;
              color: #004ca7;
            }
          }
        }
      }
    }
  }
}
/*设置表头样式*/
.station-search .table-head {
  font-size: 16px !important;
}
.station-search .el-table .caret-wrapper {
  position: absolute;
  top: 2px;
  right: 0;
}
.station-search .el-table .cell,
.el-table th div {
  padding: 0 !important;
}
.station-search .el-table tr td .cell {
  padding: 5px 2px !important;
}
.station-search .el-table .cell,
.station-search .el-table th div,
.station-search .el-table--border td:first-child .cell,
.station-search .el-table--border th:first-child .cell {
  padding-left: 0 !important;
}
/*设置表头样式*/
</style>
<style  lang="less" scoped>
.wrapper-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  > div {
    margin-bottom: 8px;
  }
}
.search-button {
  cursor: pointer;
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 70px;
  height: 27px;
 background: #FFEFEF;
  border: 1px solid #FF3819;
  border-radius: 6px;

  > span {
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #FF3819;
  }
  > img {
    width: 19px;
    height: 19px;
    margin-right: 4px;
  }
}
.edit-button {
  .search-button;
  background: #dfffef;
  border: 1px solid #02c86c;
  > span {
    color: #02c86c;
  }
}
</style>

<style lang="less" scoped>
  .assessment-container {
    width: 1875px;
    margin: 0 auto;

    .search-container {
      margin-top: 27px;
      padding-left: 43px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;

      .search-container-left,
      .search-container-right {
        display: flex;
        flex-direction: row;
      }

      .search-container-item {
        color: #004ca7;
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        margin-right: 30px;

        > span {
          margin-right: 6px;
        }
      }

      .search-button {
        width: 77px;
        height: 28px;
        line-height: 28px;
        background: #f1f8fe;
        border: 1px solid #51aef6;
        border-radius: 6px;
        cursor: pointer;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;

        > span {
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          color: #51aef6;
        }

        > img {
          width: 20px;
          height: 20px;
          margin-right: 4px;
        }
      }
    }

    .bottom-block {
      display: flex;
      flex-direction: row;
      justify-content: flex-end;

      .text {
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        line-height: 39px;
        color: #004ca7;
      }
    }

    .plan-entry-table-container {
      margin-top: 15px;

      .table-data {
        width: 100%;
        border: 0.8px solid #004ca7;
        border-radius: 9px;
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #004ca7;
      }
    }
  }
</style>

